package org.mysocial.services.business.impl;

import javax.ejb.EJB;
import javax.ejb.Stateless;

import org.mysocial.datamodel.Account;
import org.mysocial.datamodel.FriendsRing;
import org.mysocial.datamodel.Friendship;
import org.mysocial.services.business.FriendshipBusiness;
import org.mysocial.services.business.NotificationBusiness;
import org.mysocial.services.dao.FriendshipDAO;

/**
 * Session Bean implementation class FriendshipBusinessImpl
 */
@Stateless
public class FriendshipBusinessImpl implements FriendshipBusiness
{
	@EJB
	private FriendshipDAO friendshipDAO;

	@EJB
	private NotificationBusiness notificationBusiness;

	@Override
	public Friendship requestFriendship(Account fromAccount, Account toAccount,
	    FriendsRing ring)
	{
		Friendship friendship = new Friendship();
		friendship.setFromAccount(fromAccount);
		friendship.setToAccount(toAccount);
		friendship.setAccepted(false);
		friendship.setFriendRing(ring);

		friendship = friendshipDAO.persist(friendship);

		notificationBusiness.notifyFriendshipRequest(friendship);

		return friendship;
	}

	@Override
	public void acceptFriendship(Friendship friendship)
	{
		friendship.setAccepted(true);
	}

	@Override
	public void removeFriendship(Friendship friendship)
	{
		friendshipDAO.remove(friendship);
	}
}
